package tuling.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @author RenGaoshuai
 * @date 2023/12/1 11:29
 * @description 测试一个netty提供的一个数据结构
 * ByteBuf类似于一个数组，里面有两个指针，读指针，写指针。
 * 将数组分为三个区域
 * readIndex  writeIndex capacity
 * 第一段 不可操作区域
 * 第二段 可读区域
 * 第三段 可写区域
 */
public class TestBytebuf {

    public static void main(String[] args) {
        ByteBuf buffer = Unpooled.buffer(10);
        System.out.println("buffer:"+buffer);
        System.out.println("buffer readerIndex:"+buffer.readerIndex());
        System.out.println("buffer writerIndex:"+buffer.writerIndex());
        System.out.println("buffer size:"+buffer.capacity());
        System.out.println("---------------------------------------------");

        for (int i = 0; i < 5; i++) {
            buffer.writeByte(i);
        }
        System.out.println("buffer:"+buffer);
        System.out.println("buffer readerIndex:"+buffer.readerIndex());
        System.out.println("buffer writerIndex:"+buffer.writerIndex());
        System.out.println("buffer size:"+buffer.capacity());
        System.out.println("---------------------------------------------");

        for (int i = 0; i < 5; i++) {
            System.out.println(buffer.getByte(i));
        }
        System.out.println("buffer:"+buffer);
        System.out.println("buffer readerIndex:"+buffer.readerIndex());
        System.out.println("buffer writerIndex:"+buffer.writerIndex());
        System.out.println("buffer size:"+buffer.capacity());
        System.out.println("---------------------------------------------");

        for (int i = 0; i < 3; i++) {
            System.out.println(buffer.readByte());
        }
        System.out.println("buffer:"+buffer);
        System.out.println("buffer readerIndex:"+buffer.readerIndex());
        System.out.println("buffer writerIndex:"+buffer.writerIndex());
        System.out.println("buffer size:"+buffer.capacity());
        System.out.println("---------------------------------------------");

    }
}
